﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Beetle.Web.Composite;
using System.Web.UI;
using System.ComponentModel;
using Beetle.Web.Extensions;

namespace Beetle.Web.UI.WebControls
{
    /// <summary>
    /// 类说明
    /// </summary>
    /// <!-- 作者:韦腾  时间:2010年03月24日 -->
    public class CheckBox : System.Web.UI.WebControls.CheckBox, IExtensibleObject<Beetle.Web.UI.WebControls.CheckBox>, IEditStatus
    {
        #region IEditStatus 成员

        [DefaultValue(true)]
        public bool IsEditMode
        {
            get
            {
                object o = ViewState["IsEditMode"];
                return o != null ? (bool)o : true;
            }

            set
            {
                ViewState["IsEditMode"] = value;
            }
        }



        public override bool Checked
        {
            get
            {
                return base.Checked;
            }
            set
            {
                base.Checked = value;
            }
        }

        private bool? _InternalEditMode = null;
        public bool? InternalEditMode
        {
            get
            {
                return this._InternalEditMode;
            }
        }

        public void SetInternalEditModeValue(bool? b)
        {
            this._InternalEditMode = b;
        }

        public bool CheckEditMode()
        {
            if (!this.IsEditMode)
            {
                return false;
            }

            return this.GetEditMode();
        }


        #endregion

        #region IExtensibleObject<DropDownList> 成员


        private ExtensionCollection<Beetle.Web.UI.WebControls.CheckBox> extensions = null;

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtensionCollection<Beetle.Web.UI.WebControls.CheckBox> Extensions
        {
            get
            {
                return extensions;
            }
        }

        public CheckBox()
        {
            this.extensions = new ExtensionCollection<Beetle.Web.UI.WebControls.CheckBox>(this);
        }

        #endregion

        private ITemplate _readOnlyTemplate = null;
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate ReadOnlyTemplate
        {
            get
            {
                return this._readOnlyTemplate;
            }
            set
            {
                this._readOnlyTemplate = value;
                this.ChildControlsCreated = false;
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            bool isEditMode = this.CheckEditMode();
            if (!isEditMode)
            {
                //if (this.ReadOnlyTemplate == null)
                //{
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                //}
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            // this.ReadOnlyTemplate.InstantiateIn(this);
            base.Render(writer);
        }

        protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            bool isEditMode = this.CheckEditMode();
            this.ResetEditMode();
            if (isEditMode)
            {
                return base.LoadPostData(postDataKey, postCollection);
            }

            return false;
        }
    }
}
